//-----------------------------------------------------------------------
// <copyright file="ArticleCatDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class ArticleCatDAOBase
    {
        #region Common methods
        public virtual ArticleCat CreateArticleCatFromReader(IDataReader reader)
        {
            ArticleCat item = new ArticleCat();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("CategoryID"))) item.CategoryID = (long)reader["CategoryID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Name"))) item.Name = (string)reader["Name"];
                if (!reader.IsDBNull(reader.GetOrdinal("NameEn"))) item.NameEn = (string)reader["NameEn"];
                if (!reader.IsDBNull(reader.GetOrdinal("Order"))) item.Order = (int)reader["Order"];
                if (!reader.IsDBNull(reader.GetOrdinal("ProductType"))) item.ProductType = (int)reader["ProductType"];
                if (!reader.IsDBNull(reader.GetOrdinal("Desc"))) item.Desc = (string)reader["Desc"];
                if (!reader.IsDBNull(reader.GetOrdinal("Code"))) item.Code = (string)reader["Code"];
                if (!reader.IsDBNull(reader.GetOrdinal("Summary"))) item.Summary = (string)reader["Summary"];
                if (!reader.IsDBNull(reader.GetOrdinal("UserId"))) item.UserId = (long)reader["UserId"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateArticleCatFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateArticleCat methods
            
        public virtual void CreateArticleCat(ArticleCat articleCat)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spArticleCatInsert");
                
                database.AddInParameter(dbCommand, "@Name", DbType.String, articleCat.Name);
                database.AddInParameter(dbCommand, "@NameEn", DbType.String, articleCat.NameEn);
                database.AddInParameter(dbCommand, "@Order", DbType.Int32, articleCat.Order);
                database.AddInParameter(dbCommand, "@ProductType", DbType.Int32, articleCat.ProductType);
                database.AddInParameter(dbCommand, "@Desc", DbType.String, articleCat.Desc);
                database.AddInParameter(dbCommand, "@Code", DbType.String, articleCat.Code);
                database.AddInParameter(dbCommand, "@Summary", DbType.String, articleCat.Summary);
                database.AddInParameter(dbCommand, "@UserId", DbType.Int64, articleCat.UserId);
                database.AddOutParameter(dbCommand, "@CategoryID", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                articleCat.CategoryID = (long)database.GetParameterValue(dbCommand, "@CategoryID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateArticleCatException, ex);
            }
        }

        #endregion

        #region UpdateArticleCat methods
        
        public virtual void UpdateArticleCat(ArticleCat articleCat)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spArticleCatUpdate");            
                
                database.AddInParameter(dbCommand, "@CategoryID", DbType.Int64, articleCat.CategoryID);
                database.AddInParameter(dbCommand, "@Name", DbType.String, articleCat.Name);
                database.AddInParameter(dbCommand, "@NameEn", DbType.String, articleCat.NameEn);
                database.AddInParameter(dbCommand, "@Order", DbType.Int32, articleCat.Order);
                database.AddInParameter(dbCommand, "@ProductType", DbType.Int32, articleCat.ProductType);
                database.AddInParameter(dbCommand, "@Desc", DbType.String, articleCat.Desc);
                database.AddInParameter(dbCommand, "@Code", DbType.String, articleCat.Code);
                database.AddInParameter(dbCommand, "@Summary", DbType.String, articleCat.Summary);
                database.AddInParameter(dbCommand, "@UserId", DbType.Int64, articleCat.UserId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateArticleCatException, ex);
            }
        }
        
        #endregion

        #region DeleteArticleCat methods
        public virtual void DeleteArticleCat(long categoryID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spArticleCatDelete");
                
                database.AddInParameter(dbCommand, "@CategoryID", DbType.Int64, categoryID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteArticleCatException, ex);
            }
        }

        #endregion

        #region GetArticleCat methods
        
        public virtual ArticleCat GetArticleCat(long categoryID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spArticleCatGet");
                
                database.AddInParameter(dbCommand, "@CategoryID", DbType.Int64, categoryID);
                
                ArticleCat articleCat =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        articleCat = CreateArticleCatFromReader(reader);
                        reader.Close();
                    }
                }
                return articleCat;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetArticleCatException, ex);
            }
        }
        
        #endregion

        #region GetArticleCatList methods
        public virtual ArticleCatCollection GetArticleCatList(ArticleCatColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spArticleCatGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                ArticleCatCollection articleCatCollection = new ArticleCatCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        ArticleCat articleCat = CreateArticleCatFromReader(reader);
                        articleCatCollection.Add(articleCat);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return articleCatCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetArticleCatListException, ex);
            }
        }
        
        public virtual ArticleCatCollection GetArticleCatList(ArticleCatColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetArticleCatList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetArticleCatList methods
        public virtual ArticleCatCollection GetArticleCatListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spArticleCatGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                ArticleCatCollection articleCatCollection = new ArticleCatCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        ArticleCat articleCat = CreateArticleCatFromReader(reader);
                        articleCatCollection.Add(articleCat);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return articleCatCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetArticleCatListException, ex);
            }
        }
        
        public virtual ArticleCatCollection GetArticleCatListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetArticleCatListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}